package com.etime.shuhuiyuan.services.coursechapter.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.CourseChapterDao;
import com.etime.shuhuiyuan.pojo.CourseChapter;
import com.etime.shuhuiyuan.services.coursechapter.interfaces.CourseChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * service层的实现类
 */
@Service
public class CourseChapterServiceImpl implements CourseChapterService {
    /**
     * 使用spring的ico容器自动注入对象实例
     */
    @Autowired
    private CourseChapterDao courseChapterDao;
    /**
     * 添加课程章节
     *
     * @param courseChapter
     * @return
     */
    @Override
    public int insertCourseChapter(CourseChapter courseChapter) {
        //设置增加章节时间
        Long currenTime=System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
        String time=simpleDateFormat.format(currenTime);
        //把格式化的时间存入setChaptercreatetime
        courseChapter.setChaptercreatetime(time);
        return courseChapterDao.insert(courseChapter);
    }

    /**
     * 修改课程章节(根据chapterid进行修改。)
     *
     * @param courseChapter 参数中必须要有chapterid
     * @return
     */
    @Override
    public int updateCourseChapter(CourseChapter courseChapter) {
        //设置修改章节时间
        Long currentTime=System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String time=simpleDateFormat.format(currentTime);
        //把格式化的时间存入setChapterupdatetime
        courseChapter.setChapterupdatetime(time);
        return courseChapterDao.updateById(courseChapter);
    }

    /**
     * 查询课程章节列表
     *
     * @param courseChapter
     * @return
     */
    @Override
    public List<CourseChapter> selectCourseChapterList(CourseChapter courseChapter) {
        //查询条件
        QueryWrapper<CourseChapter> wrapper = new QueryWrapper<>();
        if(courseChapter!=null && courseChapter.getChaptername()!=null && !courseChapter.getChaptername().equalsIgnoreCase("")) {
            wrapper.eq("chaptername", courseChapter.getChaptername());
        }
        if(courseChapter!=null && courseChapter.getChapterstate()!=null && courseChapter.getChapterstate()!=-1){
            wrapper.eq("chapterstate",courseChapter.getChapterstate());
        }
        return courseChapterDao.selectList(wrapper);
    }

    /**
     *课程章节分页查询
     *
     * @param courseChapter
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public Page<CourseChapter> selectCourseChapterByPage(CourseChapter courseChapter, long pagenum, long pagesize) {
        //分页参数
        Page<CourseChapter> page = new Page<>(pagenum,pagesize);
        //查询条件
        QueryWrapper<CourseChapter> wrapper = new QueryWrapper<>();
        if(courseChapter!=null && courseChapter.getChaptername()!=null && !courseChapter.getChaptername().equalsIgnoreCase("")) {
            wrapper.like("chaptername", courseChapter.getChaptername());
        }
        if(courseChapter!=null && courseChapter.getChapterstate()!=null && courseChapter.getChapterstate()!=-1){
            wrapper.eq("chapterstate",courseChapter.getChapterstate());
        }

        return courseChapterDao.selectPage(page,wrapper);
    }

    /**
     * coursetypeid查询一条课程章节记录
     *
     * @param chapterid
     * @return
     */
    @Override
    public CourseChapter selectCourseChapterById(Integer chapterid) {
        return courseChapterDao.selectById(chapterid);
    }

    @Override
    public Page<CourseChapter> selectCourseChapterListByPage(CourseChapter courseChapter, long pagenum, long pagesize) {
        Page<CourseChapter> page = new Page<>(pagenum,pagesize);
        return courseChapterDao.selectChapterListByPage(page,courseChapter);
    }

    @Override
    public Page<CourseChapter> selectCourseKnotListByPage(CourseChapter courseChapter, long pagenum, long pagesize) {
        Page<CourseChapter> page = new Page<>(pagenum,pagesize);
        Integer pid = null;
        if (courseChapter!=null && courseChapter.getChapterpid()!=-1){
            pid = courseChapter.getChapterid();
        }
        return courseChapterDao.selectKnotListByPage(page,courseChapter,pid);
    }

    @Override
    public Page<CourseChapter> selectCourseKnotListByPage2(CourseChapter courseChapter, long pagenum, long pagesize, Integer pid) {
        Page<CourseChapter> page = new Page<>(pagenum,pagesize);
        return courseChapterDao.selectKnotListByPage(page,courseChapter,pid);
    }

    @Override
    public int insertCourseKnot(CourseChapter courseChapter) {
        //设置增加节点时间
        Long currenTime=System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
        String time=simpleDateFormat.format(currenTime);
        //把格式化的时间存入setChaptercreatetime
        courseChapter.setChaptercreatetime(time);
        return courseChapterDao.insert(courseChapter);
    }

    @Override
    public int updateCourseKnot(CourseChapter courseChapter) {
        //设置修改节点时间
        Long currentTime=System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String time=simpleDateFormat.format(currentTime);
        //把格式化的时间存入setChapterupdatetime
        courseChapter.setChapterupdatetime(time);
        return courseChapterDao.updateById(courseChapter);
    }

    @Override
    public CourseChapter selectCourseKnotById(Integer chapterid) {
        return courseChapterDao.selectById(chapterid);
    }

    @Override
    public List<CourseChapter> selectChapterByCourseId(CourseChapter courseChapter,Integer courseid) {
        return courseChapterDao.selectChapterByCourseId(courseChapter,courseid);
    }

}